﻿using System;
using System.Collections;
using System.Xml;
using System.Text;
using InfoJetSoft.Service.Entity;
using InfoJetSoft.Service.Util;

namespace InfoJetSoft.Service.Worker
{
	
	class FormBuildHTMLFormWorker
	{
		
		public static void buildHTMLForm(Form form)
		{
			XmlDocument doc = form.HtmlDoc;
			XmlElement root = doc.DocumentElement;

			XmlElement formElement = doc.CreateElement("div");
			formElement.SetAttribute("id", JetConstant.FormId);
			formElement.SetAttribute("style", "display:none;");
			root.AppendChild(formElement);
			
			XmlElement serviceActionElement = FormUtil.createHiddenInput(doc, JetConstant.XDOC_PARAM_ACTION, form.Action);
			formElement.AppendChild(serviceActionElement);
			XmlElement xsnIdElement = FormUtil.createHiddenInput(doc, JetConstant.XDOC_PARAM_XSN_URI, form.XsnURI);
			formElement.AppendChild(xsnIdElement);
			XmlElement commandElement = FormUtil.createHiddenInput(doc, JetConstant.XDOC_PARAM_COMMAND, "");
			formElement.AppendChild(commandElement);
			XmlElement rebuildCommandElement = FormUtil.createHiddenInput(doc, JetConstant.XDOC_PARAM_REBUILD_COMMAND, "");
			formElement.AppendChild(rebuildCommandElement);
			XmlElement xmlToEditElement = FormUtil.createHiddenInput(doc, JetConstant.XDOC_PARAM_XML_TO_EDIT_NAME, "");
			formElement.AppendChild(xmlToEditElement);
			XmlElement xmlToEditActionElement = FormUtil.createHiddenInput(doc, JetConstant.XDOC_PARAM_XML_TO_EDIT_ACTION, "");
			formElement.AppendChild(xmlToEditActionElement);
			XmlElement contextIdArrayElement = FormUtil.createHiddenInput(doc, JetConstant.XDOC_PARAM_CONTEXT_ID_ARRAY, "");
			formElement.AppendChild(contextIdArrayElement);
			XmlElement updateIdElement = FormUtil.createHiddenInput(doc, JetConstant.XDOC_PARAM_UPDATE_ID, "");
			formElement.AppendChild(updateIdElement);
			XmlElement updateFormatElement = FormUtil.createHiddenInput(doc, JetConstant.XDOC_PARAM_UPDATE_FORMAT, "");
			formElement.AppendChild(updateFormatElement);
			XmlElement xmlValueFormattedElement = FormUtil.createHiddenInput(doc, JetConstant.XDOC_PARAM_XML_VALUE_FORMATTED, "");
			formElement.AppendChild(xmlValueFormattedElement);
			XmlElement auxDomElement = FormUtil.createHiddenInput(doc, JetConstant.XDOC_PARAM_AUXDOM, "");
			formElement.AppendChild(auxDomElement);
			XmlElement ruleButtonElement = FormUtil.createHiddenInput(doc, JetConstant.XDOC_PARAM_BUTTON, "");
			formElement.AppendChild(ruleButtonElement);
			XmlElement buttonSourceElement = FormUtil.createHiddenInput(doc, JetConstant.XDOC_PARAM_BUTTON_SOURCE, "");
			formElement.AppendChild(buttonSourceElement);

			//当前表单唯一Id,用于在HttpSession中标识缓存的数据.
			XmlElement formIdElement = FormUtil.createHiddenInput(doc, JetConstant.XDOC_PARAM_FORM_ID, form.Id );
			formElement.AppendChild(formIdElement);

			//输出是否是Readonly表单,需要保持这一状态.
			XmlElement readonlyElement = FormUtil.createHiddenInput(doc, JetConstant.XDOC_PARAM_READONLY, "" + FormUtil.isReadonly( form ) );
			formElement.AppendChild(readonlyElement);

			//CID 0213 //2009.01.22 //Begin
			//用于存储OldValue.
			XmlElement oldValueElement = FormUtil.createHiddenInput(doc, JetConstant.XDOC_PARAM_OLD_VALUE, "" );
			formElement.AppendChild(oldValueElement);
			//CID 0213 //2009.01.22 //End

            //CID 0761 //2011.01.13 //Begin
            if (form.CachedImageNodeIdList != null)
            {
                string cachedImageNodeIdArray = FormUtil.buildListValue(form.CachedImageNodeIdList);
                XmlElement cachedImageNodeIdListElement = FormUtil.createHiddenInput(doc, JetConstant.XDOC_PARAM_CACHED_IMAGE_NODE_ID_ARRAY, cachedImageNodeIdArray);
                formElement.AppendChild(cachedImageNodeIdListElement);
            }
            //CID 0761 //2011.01.13 //eND

			string viewId = form.ViewId;
			if (viewId == null)
			{
				viewId = form.Manifest.DefaultView.Id;
			}
			XmlElement viewNameElement = FormUtil.createHiddenInput(doc, JetConstant.XDOC_PARAM_VIEW_ID, viewId);
			formElement.AppendChild(viewNameElement);

			if( form.Command.Equals( JetConstant.CMD_BUILD_BY_TEMPLATE ) )
			{
				XmlElement initiatorFormElement = FormUtil.createHiddenInput(doc, JetConstant.XDOC_PARAM_INITIATOR_FORM, JetConstant.YES );
				formElement.AppendChild(initiatorFormElement);
			}
			else
			{
				string initiatorForm = (string)form.ParamTable[ JetConstant.XDOC_PARAM_INITIATOR_FORM ];
				if( initiatorForm != null )
				{
					XmlElement initiatorFormElement = FormUtil.createHiddenInput(doc, JetConstant.XDOC_PARAM_INITIATOR_FORM, JetConstant.YES );
					formElement.AppendChild(initiatorFormElement);
				}
			}
			
			ArrayList piFieldList = form.PiFieldList;
			ArrayList fieldList = form.FieldList;

            //CID 0664 //2010.10.05 //Begin
            ArrayList formFieldHTMLElementList = new ArrayList();
            //CID 0664 //2010.10.05 //End
			
			if (piFieldList == null || fieldList == null)
			{
				return ;
			}
			for (int i = 0; i < piFieldList.Count; i++)
			{
				Field field = (Field) piFieldList[ i ];
				XmlElement inputElement = FormUtil.createHiddenInput(doc, JetConstant.PIPREFIX + field.Name, field.Value);
				formElement.AppendChild(inputElement);
                //CID 0664 //2010.10.05 //Begin
                formFieldHTMLElementList.Add(inputElement);
                //CID 0664 //2010.10.05 //End
			}

			for (int i = 0; i < fieldList.Count; i++)
			{
				Field field = (Field) fieldList[ i ];
				//CID 0331 //2009.07.22 //Begin
				if( field.IsRichText )
				{
					XmlElement textAreaElemeent = FormUtil.createHiddenTextArea(doc, JetConstant.DocId + field.Name, field.Value);
					formElement.AppendChild(textAreaElemeent);
                    //CID 0664 //2010.10.05 //Begin
                    formFieldHTMLElementList.Add(textAreaElemeent);
                    //CID 0664 //2010.10.05 //End
				}
				else
				{
					XmlElement inputElement = FormUtil.createHiddenInput(doc, JetConstant.DocId + field.Name, field.Value);
					formElement.AppendChild(inputElement);
                    //CID 0664 //2010.10.05 //Begin
                    formFieldHTMLElementList.Add(inputElement);
                    //CID 0664 //2010.10.05 //End
				}
				//CID 0331 //2009.07.22 //End
			}

            //CID 0664 //2010.10.05 //Begin
            if (form.Manifest.FormFieldsCacheEnabled)
            {
                form.FormFieldHTMLElementList = formFieldHTMLElementList;
            }
            //CID 0664 //2010.10.05 //End
			
			string masterDetialValue = (string) form.ParamTable[JetConstant.XDOC_PARAM_MASTER_DETIAL];
			if (masterDetialValue == null || masterDetialValue.Length <= 0)
			{
				View view = form.Manifest.getView(form.ViewId);
				ArrayList masterDetialList = view.MasterNameList;
				StringBuilder valueBuffer = new StringBuilder();
				for (int i = 0; i < masterDetialList.Count; i ++)
				{
					string masterName = (string) masterDetialList[ i ];
					valueBuffer.Append(masterName + JetConstant.INNER_SEPARATOR + "1");
					if (i < masterDetialList.Count - 1)
					{
						valueBuffer.Append(JetConstant.ID_SEPARATOR);
					}
				}
				masterDetialValue = valueBuffer.ToString();
			}
			
			XmlElement masterDetialElement = FormUtil.createHiddenInput(doc, JetConstant.XDOC_PARAM_MASTER_DETIAL, masterDetialValue);
			formElement.AppendChild(masterDetialElement);

			//当SessionCache启用时,不再将IsDataObjectPageCache为true的DataObject输出到页面上.
			if( !ConfigUtil.EnabledSessionCache() ) 
			{
				int dataObjectIndex = 1;
				foreach( string key in form.DataTable.Keys )
				{
					if( form.Manifest.FormConfiguration.IsDataObjectPageCache( key ) )
					{
						XmlDocument dataDoc = (XmlDocument)form.DataTable[ key ];
						string dataXml = key + "\n" + dataDoc.OuterXml;
						XmlElement dataXmlElement = FormUtil.createHiddenInput(doc, JetConstant.XDOC_PARAM_DATAOBJECT_XML + dataObjectIndex, dataXml);
						formElement.AppendChild(dataXmlElement);
						dataObjectIndex ++;
					}
				}
			}

			//分析AJAX Bracket,用于在EditPart中使用AJAX模式,因为SharePoint中AJAX响应中包含其它内容,所以使用括号包裹起来.
			string ajaxBracket = FormUtil.parseAjaxBracket( form );
			XmlElement ajaxBracketElement = FormUtil.createHiddenInput(doc, JetConstant.XDOC_PARAM_AJAX_BRACKET, ajaxBracket);
			formElement.AppendChild(ajaxBracketElement);

			//保持ReplaceBrowserContextMenu和DisplayMenuArrow的false状态.
			//表单提交后,在FormBuildHTMLScriptWorker中判断是否已经设置了false状态.
            //CID 1554 //2012.12.24 //Begin
			if( (form.JetContext.ReplaceBrowserContextMenu) || (form.ParamTable[ JetConstant.XDOC_PARAM_REPLACE_CONTEXT_MENU ] != null) )
			{
				XmlElement replaceContextMenuElement = FormUtil.createHiddenInput(doc, JetConstant.XDOC_PARAM_REPLACE_CONTEXT_MENU, JetConstant.HTML_true);
				formElement.AppendChild(replaceContextMenuElement);
			}
            //CID 1554 //2012.12.24 //End
			if( (!form.JetContext.DisplayMenuArrow) || (form.ParamTable[ JetConstant.XDOC_PARAM_DISPLAY_MENU_ARROW ] != null) )
			{
				XmlElement displayMenuArrowMenuElement = FormUtil.createHiddenInput(doc, JetConstant.XDOC_PARAM_DISPLAY_MENU_ARROW, JetConstant.HTML_false);
				formElement.AppendChild(displayMenuArrowMenuElement);
			}

            //CID 2166 //2014.02.25 //Begin
            if (form.JetContext.IsListAttachmentsReadonly || (form.ParamTable[ JetConstant.XDOC_PARAM_LIST_ATTACHMENTS_READONLY ] != null) )
            {
                XmlElement listAttachmentReadonlyElement = FormUtil.createHiddenInput(doc, JetConstant.XDOC_PARAM_LIST_ATTACHMENTS_READONLY, JetConstant.HTML_true);
                formElement.AppendChild(listAttachmentReadonlyElement);
            }
            //CID 2166 //2014.02.25 //End
		}
	}
}